import asyncio
import time

'''方法1'''
# async def func01():
#     print("Tom正在睡觉")
#     # time.sleep(4) # 当程序出现IO时，程序执行同步操作
#     await asyncio.sleep(4)  # 异步操作IO代码
#     print("Tom起床了")
# 
# 
# async def func02():
#     print("Any还在打游戏")
#     # time.sleep(2)
#     await asyncio.sleep(2)
#     print("Any睡觉了") 
# 
# 
# async def func03():
#     print("Gou在美国")
#     await asyncio.sleep(6)
#     print("Gou回中国了")
# 
# 
# if __name__ == '__main__':
#     # start = asyncio.asyncio()
#     # func01()
#     # func02()
#     # end = asyncio.asyncio()
#     # print(end-start)
# 
#     start = time.time()
#     g1 = func01()
#     g2 = func02()
#     g3 = func03()
#     task = [g1, g2, g3]
#     # 一次性启动多个协程任务
#     asyncio.run(asyncio.wait(task))
#     end = time.time()
#     print(end - start)


"""方法2"""


async def func01():
    print("Tom正在睡觉")
    # time.sleep(4) # 当程序出现IO时，程序执行同步操作
    await asyncio.sleep(4)  # 异步操作IO代码
    print("Tom起床了")


async def func02():
    print("Any还在打游戏")
    # time.sleep(2)
    await asyncio.sleep(2)
    print("Any睡觉了")


async def func03():
    print("Gou在美国")
    await asyncio.sleep(6)
    print("Gou回中国了")


# 协程主函数，将所有协程函数存放在协程main函数中运行
async def main():
    # # 第一种写法【不推荐】
    # g1 = func01()
    # g2 = func02()
    # g3 = func03()
    # await g1  # 一般关键字await放在协程对象前面
    # await g2  # 一般关键字await放在协程对象前面
    # await g3  # 一般关键字await放在协程对象前面

    """------------------------------------------------------------"""

    # 第二种写法(推荐)
    task = [
        func02(),
        func03(),
        func01()
    ]
    await asyncio.wait(task) # 运行时间>>> 6.022056818008423


if __name__ == '__main__':
    start = time.time()
    asyncio.run(main())
    end = time.time()
    print(end-start)
